<?php
class test extends parent_class
{
	public $id=0;
	public $student=0;
	public $test=0;
	public $questions="";
	public $result=0;
	
	public function __construct($id=0)
	{
		if ($id>0)
		{
			return $this->get_byid($id);
		}
	}

	public function get($id=0,$test=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		$id=intval($id);
		
		$query="SELECT * FROM `tests` WHERE `student`='".$id."' and `test`='".$test." 'LIMIT 1";
		if ($result=db::$db->query($query))
		{
			if($result->num_rows==1)
			{
				$row=$result->fetch_object();
				$this->id=$row->id;
				$this->student=$row->student;
				$this->test=$row->test;
				$this->questions=$row->questions;
				$this->result=$row->result;
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			$this->error(2);
		}
	}
	
	public function get_byid($id=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		$id=intval($id);
		
		$query="SELECT * FROM `tests` WHERE `id`='".$id."'";
		if ($result=db::$db->query($query))
		{
			if($result->num_rows==1)
			{
				$row=$result->fetch_object();
				$this->id=$row->id;
				$this->student=$row->student;
				$this->test=$row->test;
				$this->questions=$row->questions;
				$this->result=$row->result;
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			$this->error(2);
		}
	}
	public function remove($id=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		if (!($this->exist()))
		{
			return false;
		}
		
		$query="DELETE FROM `tests` WHERE `id`='".intval($id)."'LIMIT 1";
		
		if (db::$db->query($query))
		{
			return true;
		}
		else
		{
			$this->error(2);
		}
	}
	
	public function add()
	{
		$query="INSERT INTO `tests` VALUES 
				(NULL,'$this->student','$this->test','$this->question','$this->result')";
		if (db::$db->query($query))
		{
			return true;
		}
		else
		{
			$this->error(2);
		}
	}

	public function update($id=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		$query="UPDATE `tests` SET `student`='$this->student', 
									`test`='$this->test', `questions`='$this->questions',`result`='$this->result' WHERE `id`='".$id."'";
		if ($result=db::$db->query($query))
		{
			return true;
		}
		else
		{
			$this->error(2);
		}
	}
	
	public function exist($id=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		$query="SELECT `id` FROM `student` WHERE `id`='$id' LIMIT 1";
		if ($result=db::$db->query($query))
		{
			if ($result->num_rows==1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			$this->error(2);
		}
	}
	public function returnit($str="")
	{
		$parse=new parser;
		$parse->additem("%id%",$this->id);
		$parse->additem("%text%",$this->text);
		$parse->additem("%type%",$this->type);
		$parse->additem("%answer%",$this->answer);
		$parse->additem("%answer_mask%",$this->answer_mask);
		$parse->additem("%article%",$this->article);
		return $parse->parse($str);
	}
}
?>